package com.fangs.yehwangglobal.net.rx;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by caih on 2018/4/12.
 */
public class MyRxUtil {
    //执行rx任务
    public static <R> void executeRxTask(MyRxTask<R> t) {

        Observable.create(new MyRxOnSubscribe<MyRxTask<R>>(t) {
            @Override
            public void subscribe(ObservableEmitter<MyRxTask<R>> emitter) {
                try {
                    getT().setR(getT().doInIOThread());
                    emitter.onNext(getT());
                    emitter.onComplete();
                } catch (Exception ignored) {
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MyRxTask<R>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(MyRxTask<R> rMyRxTask) {
                        rMyRxTask.doInUIThread(rMyRxTask.getR());
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
